////////////////////////////////////
//// ASKIMA - COLLABORATOR PROFILE
//// COMMON FUNCTIONS
////////////////////////////////////

// CONSTANTS

// The frequency at which refreshes notifications display (in seconds)
var NOTIF_REFRESH_FREQUENCY = 10;

// //////////////////////
// GLOBAL VARIABLES
// //////////////////////

// Missions and formations
var selectedMissionSkills = new Array();
var selectedFormationSkills = new Array();
var selectedUpdateMissionSkills = new Array();
var selectedUpdateFormationSkills = new Array();

var skills = new Array();
var nationalities = new Array();
var notifications = new Array();

var missionInputID = -1;
var formationInputID = -1;
var missionUpdateInputID = -1;
var formationUpdateInputID = -1;
var notificationsCounter = 0;

var missionTable;
var formationTable;

// Languages

var languagesLineIDPrefix = "languageInput_";
var languagesLineID = -1;

var languages = new Array();
var selectedLanguages = {};

// ///////////////////////////
// GENERIC METHODS
// ///////////////////////////

$(document).ready(
		function() {
			// The date picker concerning the collaborator birthday
			$('#birthday').datepicker({
				dateFormat : 'dd/mm/yy',
				changeMonth : true,
				changeYear : true,
				showAnim : 'fold',
				yearRange : '1950:2050'
			});
			// The date picker concerning the collaborator hiring date
			$('#hiring').datepicker({
				dateFormat : 'dd/mm/yy',
				changeMonth : true,
				changeYear : true,
				showAnim : 'fold',
				yearRange : '1950:2050'
			});
			// Table which display missions
			missionTable = $('#missions_table').dataTable({
				"bJQueryUI" : true,
				"bFilter" : false,
				"bSearchable" : false,
				"bPaginate" : false,
				"bInfo" : false
			});
			// Table which display formations
			formationTable = $('#formations_table').dataTable({
				"bJQueryUI" : true,
				"bFilter" : false,
				"bSearchable" : false,
				"bPaginate" : false,
				"bInfo" : false
			});

			// Init auto complete nationalities list
			$.getJSON("nationalitiesToJSON", function(data) {
				$.each(data, function(i, val) {
					var name = val.name;
					nationalities.push(name);
				});
				initNationalitiesAutoComplete();
			});

			// Init auto complete skills list
			initSkillAutoComplete();

			// Init auto complete languages list
			initLanguagesAutoComplete();

			// Notification system
			$.extend($.gritter.options, {
				position : 'bottom-left'
			});
			retrieveNotifications();
			var timer = setInterval("retrieveNotifications()",
					NOTIF_REFRESH_FREQUENCY * 1000);

		});

/**
 * Hide or display a panel
 * 
 * @param identifiant
 * @param identifiant_bt
 */

function hide(identifiant, identifiant_bt) {
	if (document.getElementById(identifiant).className == "hidden") {
		$('#' + identifiant).slideDown();
		document.getElementById(identifiant).className = "visible";
		document.getElementById(identifiant_bt).value = "Masquer";
	} else {
		$('#' + identifiant).slideUp();
		document.getElementById(identifiant).className = "hidden";
		document.getElementById(identifiant_bt).value = "Afficher";
	}
}

// ////////////////////////////////////////////////////////////////////
// SKILL INPUTS MANAGEMENT (METHODS COMMON TO MISSION AND FORMATION)
// ////////////////////////////////////////////////////////////////////

/**
 * Load the auto complete (oracle) widget (with categories support)
 */

function initSkillAutoComplete() {

	// Configure the widget to support categories
	$.widget("custom.catcomplete", $.ui.autocomplete, {
		_renderMenu : function(ul, items) {
			var self = this, currentCategory = "";
			$.each(items, function(index, item) {
				if (item.category != currentCategory) {
					ul.append("<li class='ui-autocomplete-category'><b>"
							+ item.category + "</b></li>");
					currentCategory = item.category;
				}
				self._renderItem(ul, item);
			});
		}
	});

	// Load data from server (by JSON)
	$.getJSON("skillsToJSON", function(data) {
		$.each(data, function(i, val) {
			var name = val.name;
			var category = val.category.skillCategoryName;
			skills.push({
				label : name,
				category : category
			});
		});
		$(".skillInput").catcomplete({
			delay : 0,
			source : skills
		});
	});
}

/**
 * Load auto complete (oracle) functionalities on (mission and formations)
 * skills inputs
 */

function loadSkillAutoComplete() {
	$(".skillInput").catcomplete({
		delay : 0,
		source : skills
	});
}

/**
 * Add a auto complete (oracle) skill input into the panel
 */

function addSkillInput(selectedArray, inputPrefix, inputCounter, panelID) {
	// Generate a new input id
	var id = (++(inputCounter.value));
	var namedID = inputPrefix + id;

	// Add the new input
	$("#" + panelID).append(
			'<br/><input id="' + namedID
					+ '" type="text" size="20" class="skillInput"/>');

	// Load auto complete entries
	loadSkillAutoComplete();
}

/**
 * Removes an skill input (generic function)
 * 
 * @param selectedArray
 * @param inputCounter
 * @param inputPrefix
 */

function removeSkillInput(selectedArray, inputCounter, inputPrefix, panelID) {

	if (inputCounter.value >= 0) {
		var id = inputPrefix + (inputCounter.value--);
	} else {
		var id = inputPrefix + (inputCounter.value);
	}
	// Remove the <br/>
	$("#" + panelID + " > #" + id).prev().remove();
	// Remove the input
	$("#" + panelID + " > #" + id).remove();
}

// ////////////////////////////////////////////////////////////////////
// LANGUAGES INPUTS MANAGEMENT (POTENTIALLY COMMONS)
// ////////////////////////////////////////////////////////////////////

/**
 * Initialize the languages list (oracle & auto complete mode)
 */

function initLanguagesAutoComplete() {
	if (languages.length == 0) {
		$.getJSON("languagesToJSON", function(data) {
			$.each(data, function(i, val) {
				var name = val.name;
				languages.push(name);
			});
		});
	}
}